package com.lujianing.test.flyingsaucer;


import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.BaseFont;
import com.lujianing.test.util.PathUtil;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.*;
import java.util.Properties;

/**
 * Created by lujianing on 2017/5/7.
 */
public class JavaToPdfHtmlVelocity {

    private static final String DEST = "target/HelloWorld_CN_HTML_VELOCITY_FS.pdf";
    private static final String VM = "template_velocity_fs.vm";
    private static final String FONT = "simhei.ttf";
    private static final String LOGO_PATH = "file://"+PathUtil.getCurrentPath()+"/logo.png";

    private static VelocityEngine engine = null;

    static {
        Properties properties = new Properties();
        properties.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, PathUtil.getCurrentPath());
        properties.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        properties.setProperty(RuntimeConstants.INPUT_ENCODING, "UTF-8");
        properties.setProperty(RuntimeConstants.OUTPUT_ENCODING, "UTF-8");
        engine = new VelocityEngine();
        engine.init(properties);
    }

    public static void main(String[] args) throws IOException, DocumentException, com.lowagie.text.DocumentException {
        VelocityContext ctx = new VelocityContext();
        ctx.put("name","鲁家宁");
        String content = JavaToPdfHtmlVelocity.velocityRender(ctx,VM);
        JavaToPdfHtmlVelocity.createPdf(content,DEST);
    }

    /**
     * velocity渲染html
     */
    public static String velocityRender(VelocityContext ctx, String htmlTmp) {
        Writer out = new StringWriter();
        try {
            // 获取模板
            org.apache.velocity.Template template = engine.getTemplate(htmlTmp);
            // 合并数据模型与模板
            template.merge(ctx, out); //将合并后的数据和模板写入到流中，这里使用的字符流
            out.flush();
            return out.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return null;
    }

    public static void createPdf(String content,String dest) throws IOException, DocumentException, com.lowagie.text.DocumentException {
        ITextRenderer render = new ITextRenderer();
        ITextFontResolver fontResolver = render.getFontResolver();
        fontResolver.addFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        // 解析html生成pdf
        render.setDocumentFromString(content);
        //解决图片相对路径的问题
        render.getSharedContext().setBaseURL(LOGO_PATH);
        render.layout();
        render.createPDF(new FileOutputStream(dest));
    }
}
